package com.xmut.ZhuCaiDan;

import java.awt.*;
import javax.swing.*;

import com.sql.DAO;

import com.Model.Pojo.Users;

import java.awt.event.*;

public class ZhuMianBan extends JPanel{
	private int yue;
	private String zhanghao,mima,xingming,youxiang,shoujihao;
	private JLabel huanyinglabel;
	private JLabel zhanghaolabel,mimalabel;
	private JLabel xingminglabel,youxianglabel,shoujihaolabel;
	private JLabel yuelabel;
	private JTextField xingmingkuang,youxiangkuang,shoujihaokuang;
	private JButton cunqian,qvqian,xiugai,querenxiugai;
	
	public ZhuMianBan(){}
	public ZhuMianBan(Users user){
		zhanghao = user.getZhanghao();
		mima = user.getMima();
		int num = mima.length()-1;
		mima = mima.substring(0,num);
		yue = user.getYue();
		xingming = user.getXingming();
		youxiang = user.getYouxiang();
		shoujihao = user.getShoujihao();
		huanyinglabel = new JLabel("欢迎回来，" + xingming);
		zhanghaolabel = new JLabel("账号：" + zhanghao);
		mimalabel = new JLabel("密码：" + mima);
		yuelabel = new JLabel("您的余额为：" + yue);
		xingminglabel = new JLabel("姓名：" + xingming);
		youxianglabel = new JLabel("年龄：" + youxiang);
		shoujihaolabel = new JLabel("手机：" + shoujihao);
		cunqian = new JButton("存钱");
		qvqian = new JButton("取钱");
		xiugai = new JButton("修改信息");
		querenxiugai = new JButton("确认修改");
		xingmingkuang = new JTextField();
		youxiangkuang = new JTextField();
		shoujihaokuang = new JTextField();
		cunqian.addActionListener(new ButtonListener());
		qvqian.addActionListener(new ButtonListener2());
		xiugai.addActionListener(new ButtonListener3());
		
		this.setLayout(null);//不使用布局
		huanyinglabel.setBounds(40,20,300,30);
		yuelabel.setBounds(40,60,300,30);
		cunqian.setBounds(30,100,100,30);
		qvqian.setBounds(140,100,100,30);
		zhanghaolabel.setBounds(300,20,300,30);
		mimalabel.setBounds(300,60,300,30);
		xingminglabel.setBounds(300,100,300,30);
		xingmingkuang.setBounds(335,140,200,30);
		youxianglabel.setBounds(300,180,300,30);
		youxiangkuang.setBounds(335,220,200,30);
		shoujihaolabel.setBounds(300,260,300,30);
		shoujihaokuang.setBounds(335,300,200,30);
		xiugai.setBounds(330,350,110,30);
//		querenxiugai.setBounds(460,220,110,30);
		
		add(huanyinglabel);
		add(yuelabel);
		add(cunqian);
		add(qvqian);
		add(zhanghaolabel);
		add(mimalabel);
		add(xingminglabel);
		add(youxianglabel);
		add(shoujihaolabel);
		add(xiugai);
		add(xingmingkuang);
		add(youxiangkuang);
		add(shoujihaokuang);
//		add(querenxiugai);
//		xingmingkuang.setVisible(false);
//		youxiangkuang.setVisible(false);
//		shoujihaokuang.setVisible(false);
//		querenxiugai.setVisible(false);
		
		setPreferredSize(new Dimension(600,500));
		setBackground(Color.white);
	}

	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e){
			String s=JOptionPane.showInputDialog("请输入存钱的数量:");
			int a = Integer.parseInt(s);
			yue+=a;
			yuelabel.setText("您的余额为：" + yue);
			DAO dao = new DAO();
			String sql = "update users set yue='" + yue + "' where name='" + zhanghao + "' ";
			dao.xiugai(sql);
		}
	}
	private class ButtonListener2 implements ActionListener
	{
		public void actionPerformed(ActionEvent e){
			String s=JOptionPane.showInputDialog("请输入存钱的数量:");
			int a = Integer.parseInt(s);
			yue-=a;
			if(yue < 0){
				yue+=a;
				JOptionPane.showMessageDialog(null, "请确保取款的数量小于存款", "存款不足", JOptionPane.ERROR_MESSAGE);
			}
			yuelabel.setText("您的余额为：" + yue);
			DAO dao = new DAO();
			String sql = "update users set yue='" + yue + "' where name='" + zhanghao + "' ";
			dao.xiugai(sql);
		}
	}
	
	private class ButtonListener3 implements ActionListener
	{
		public void actionPerformed(ActionEvent e){
//			xingmingkuang.setVisible(true);
//			xingmingkuang.setBorder(getBorder());
//			youxiangkuang.setVisible(true);
//			shoujihaokuang.setVisible(true);
//			querenxiugai.setVisible(true);
//			querenxiugai.addActionListener(new ButtonListener4());
//			xiugai.setText("取消修改");
//			xiugai.addActionListener(new ButtonListener5());
			String str = new String("");
			if(str.equals(xingmingkuang.getText()) == false){
				DAO dao = new DAO();
				String sql = "update users set xingming='" + xingmingkuang.getText() + "' where name='" + zhanghao + "' ";
				dao.xiugai(sql);
				xingming = xingmingkuang.getText();
				huanyinglabel.setText("欢迎回来，" + xingming);
				xingminglabel.setText("姓名：" + xingming);
				xingmingkuang.setText("");
			}
			if(str.equals(youxiangkuang.getText()) == false){
				DAO dao = new DAO();
				String sql = "update users set youxiang='" + youxiangkuang.getText() + "' where name='" + zhanghao + "' ";
				dao.xiugai(sql);
				youxiang = youxiangkuang.getText();
				youxianglabel.setText("年龄：" + youxiang);
				youxiangkuang.setText("");
			}
			if(str.equals(shoujihaokuang.getText()) == false){
				DAO dao = new DAO();
				String sql = "update users set shoujihao='" + shoujihaokuang.getText() + "' where name='" + zhanghao + "' ";
				dao.xiugai(sql);
				shoujihao = shoujihaokuang.getText();
				shoujihaolabel.setText("手机：" + shoujihao);
				shoujihaokuang.setText("");
			}
		}
	}
}
